<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="评价管理">
            <div style="text-align: right; margin-bottom: 20px">
              <el-button
                icon="el-icon-plus"
                type="primary"
                size="small"
                @click="clickAdd(1, '0')"
                >新增</el-button
              >
            </div>
            <el-table row-key="id" :data="list" border>
              <el-table-column label="序号" prop="name" width="70">
                <template #default="{ row }">
                  {{ row.id }}
                </template>
              </el-table-column>
              <el-table-column label="评价正文" prop="content" />
              <el-table-column label="发布评价的用户" prop="userId" />
              <el-table-column
                label="本次评价所属国际订单id"
                prop="internationalOrderId"
                width="200"
              />
              <el-table-column label="发布评论的时间" prop="createTime" />
              <el-table-column label="用户名" prop="username" />
              <el-table-column label="用户头像" prop="userAvatar" />
              <el-table-column label="审核是否通过" prop="state" />
              <el-table-column label="操作">
                <template slot-scope="scope" fixed="right">
                  <el-button
                    size="small"
                    type="primary"
                    @click="clickInquire(1, '0')"
                    >审核</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="handleDelete(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <span>确定删除吗？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="deleteComfirm">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 弹框 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form v-loading="loading" label-width="200px">
          <el-form-item v-if="formData.id" label="id" prop="id">
            <el-input v-model="formData.id" type="number" disabled />
          </el-form-item>
          <el-form-item label="评论正文" prop="content">
            <el-input v-model="formData.content" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="发布评论的用户" prop="userId">
            <el-input v-model="formData.userId" placeholder="请输入" />
          </el-form-item>
          <el-form-item
            label="本次评价所属国际订单id"
            prop="internationalOrderId"
          >
            <el-input
              v-model="formData.internationalOrderId"
              placeholder="请输入"
            />
          </el-form-item>

          <el-form-item label="发布评价的时间" prop="createTime">
            <el-input v-model="formData.createTime" placeholder="请输入" />
          </el-form-item>

          <el-form-item label="用户名" prop="username">
            <el-input v-model="formData.username" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="审核是否通过" prop="state">
            <el-input v-model="formData.state" placeholder="请输入" />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确认</el-button>
          </div>
        </template>
      </el-dialog>

      <!-- 审核弹框 -->
      <el-dialog title="审核" :visible="samllDialog" @close="closeSamllDialog">
        <span>是否审核通过该条评价？</span>
        <template #footer>
          <div style="text-align: right">
            <el-button @click="clickPass">否</el-button>
            <el-button type="primary">是</el-button>
          </div>
        </template>
      </el-dialog>
    </div>

    <!-- 分页 -->
    <div align="center">
      <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="list.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { selectEvaluate, insertEvaluate, updateEvaluate, deleteEvaluate,checkEvaluate} from '@/api/role'
export default {
  name: 'Permission',
  data() {
    return {
      dialogVisible: false,
      // 分页数据
      currentPage: 1,
      list: [],
      pageParam: { // 分页参数对象
        page: 1,
        num: 10
      },
      pageSizeArr: [5, 10, 20, 50],
      dataTotal: 0, // 分页组件数据总条数
      showDialog: false,
      //审核弹框
      samllDialog: false,
      formData: {
        content: '', // 名称
        userId: '',
        internationalOrderId: '',
        receivername: '',
        createTime: '',
        username: '',
        state: ''

      },
      loading: false,
      permissionData: [] // 存放用于展示的权限列表数据 (树形结构)
    }
  },
  computed: {
    showTitle() {
      if (this.formData.id) {
        return '审核'
      } else {
        return '添加'
      }
    }
  },
  mounted() {
    console.log('进入页面的钩子函数')
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const data = await selectEvaluate(this.pageParam)
      console.log('请求响应结果', data)
      this.list = data.list
      this.dataTotal = data.total
    },
    clickAdd(type, pid) {
      this.showDialog = true
    },
    // 审核弹窗
    clickInquire() {
      this.samllDialog = true
    },
    closeDialog() {
      this.showDialog = false
      // 重置清空数据
      this.formData = {
      }
    },
    // 点击弹框的确认按钮
    async clickSubmit() {
      if (this.formData.content === '' || this.formData.userId === '') {
        this.$message.error('评论正文和发布评论的用户不能为空')
        return
      }
      if (this.formData.id) {
        // 编辑操作
        const { postalcode, ...formData } = this.formData
        await updateEvaluate(formData)
      } else {
        // 添加操作
        await insertEvaluate(this.formData)
      }
      this.showDialog = false
      this.$message.success('操作成功')
      this.getPermissionList()
    },
    // 关闭审核弹窗
    closeSamllDialog() {
        this.samllDialog = false
    },

    clickPass() {
        
    },

    // 删除功能
    handleDelete(id) {
      this.deleteId = id
      this.dialogVisible = true
    },
    async deleteComfirm() {
      this.dialogVisible = false
      await deleteEvaluate({
        id: this.deleteId
      })
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getPermissionList()
    },
    handleEdit(row) {
      console.log(row)
      // const {postalCode,...formData} = row
      // formData.postalcode = postalCode
      this.formData = row
      this.formData.postalcode = row.postalCode
      this.showDialog = true
    },
    // 点击查看编辑按钮, 显示弹框
    clickEdit(id) {
      // 显示弹框
      this.showDialog = true
      this.loading = true
      // 发送请求, 获取数据, 进行回显
      // const { data } = await reqGetPermissionDetail(id)
      // this.formData = data

      this.loading = false
    },

    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style>
</style>
